package com.polaris.live.dialog

import android.content.Context
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.HonorDialogAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.databinding.DialogHonorBannerBinding
import com.polaris.live.manager.DownFileManager
import com.polaris.live.resp.back_resp.UserMedalVo
import com.polaris.live.utils.file.FileUtils
import com.zhpan.indicator.enums.IndicatorSlideMode
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.concurrent.ConcurrentLinkedQueue

/**
 * HonorBannerDialog
 *
 * @author Created by 半仙 on 2024/1/29/029 14:44
 */
class HonorBannerDialog(
    context: Context,
    private val mMedals: List<UserMedalVo>,
    private val mPosition: Int,
    private val mNeedRouter: Boolean = false,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogHonorBannerBinding

    private val mAdapter by lazy { HonorDialogAdapter(mNeedRouter, lifecycleScope) }

    private var mLoadJob: Job? = null

    private val mLoadList = ConcurrentLinkedQueue<UserMedalVo>()
    override fun getImplLayoutId() = R.layout.dialog_honor_banner
    override fun onCreate() {
        super.onCreate()
        mBinding = DialogHonorBannerBinding.bind(popupImplView)
        mBinding.levelBanner.apply {
            registerLifecycleObserver(lifecycle)
            setAdapter(mAdapter)
            setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorVisibility(View.GONE)
            setRoundCorner(12.dp)
            setAutoPlay(false)
        }

        mBinding.bannerChangeLast.setOnClickListener {
            if (mMedals.isEmpty()) return@setOnClickListener
            val getNow = mBinding.levelBanner.currentItem
            mBinding.levelBanner.setCurrentItem(getNow - 1, true)
        }
        mBinding.bannerChangeNext.setOnClickListener {
            if (mMedals.isEmpty()) return@setOnClickListener
            val getNow = mBinding.levelBanner.currentItem
            mBinding.levelBanner.setCurrentItem(getNow + 1, true)
        }
        mBinding.levelBanner.create(mMedals)
        mBinding.ivClose.setOnSingleClickListener {
            dismiss()
        }
        mBinding.levelBanner.setCurrentItem(mPosition, false)

        mMedals.forEach {
            val path = it.pagUrl?.let { it1 -> FileUtils.getFilePath(FileUtils.getPagFile(), it1) }
            if (path != null && !File(path).exists()) {
                mLoadList.add(it)
            }
        }
        loadPag()
        mAdapter.setOnClickBackListener {
            dismiss()
        }
    }

    private fun loadPag() {
        if (mLoadList.isEmpty()) return
        val poll = mLoadList.poll()
        if (poll == null) {
            loadPag()
            return
        }
        mLoadJob = CoroutineScopeManager.ioScope.launch {
            poll.pagUrl?.let { DownFileManager.loadFileCallback(FileUtils.getPagFile(), it) }
            withContext(CoroutineScopeManager.mainDispatcher) {
                val item = mBinding.levelBanner.currentItem
                mBinding.levelBanner.create(mMedals)
                if (item != null) {
                    mBinding.levelBanner.setCurrentItem(item, false)
                }
                loadPag()
            }
        }
    }

    override fun onDismiss() {
        mLoadJob?.cancel()
        mLoadJob == null
        super.onDismiss()
    }

}